package io.renren.modules.carlunbo.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.carlunbo.dto.CarLunboDTO;
import io.renren.modules.carlunbo.vo.CarLunboVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.carlunbo.entity.CarLunboEntity;
import io.renren.modules.carlunbo.service.CarLunboService;




/**
 * 首页轮播图管理
 *
 */
@Api(tags = "轮播图后台管理接口")
@RestController
@RequestMapping("/carlunbo")
public class CarLunboController {
    @Autowired
    private CarLunboService carLunboService;

    @ApiOperation("轮播图列表展示(分页---后台接口)")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageData<CarLunboVO> pageData = carLunboService.getLunboList(params);
        return R.ok().put("page", pageData);
    }


    /**
     * 轮播图根据id查看接口(后台接口)
     */
    @GetMapping("/info/{id}")
    @ApiOperation("轮播图根据id查看接口(后台接口)")
    public R info(@PathVariable("id") Long id){
		CarLunboEntity carLunbo = carLunboService.getById(id);
        return R.ok().put("carLunbo", JacksonUtils.obj2pojo(carLunbo,CarLunboVO.class));
    }

    /**
     * 轮播图新增(后台接口)
     */
    @PostMapping
    @ApiOperation("轮播图新增(后台接口)")
    public R save(@RequestBody CarLunboDTO dto){
        // 验证参数
        ValidatorUtils.validateEntity(dto, AddGroup.class);
		carLunboService.saveCarLunbo(dto);
        return R.ok();
    }

    /**
     * 轮播图修改接口(后台接口)
     */
    @PutMapping
    @ApiOperation("轮播图修改接口(后台接口)")
    public R update(@RequestBody CarLunboDTO dto){
        // 验证参数
        ValidatorUtils.validateEntity(dto, AddGroup.class);
		carLunboService.updateCarLunbo(dto);
        return R.ok();
    }

    /**
     * 轮播图删除接口(后台接口)
     */
    @DeleteMapping
    @ApiOperation("轮播图删除接口(后台接口)")
    public R delete(@RequestBody Long[] ids){
		carLunboService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 轮播图启用/禁用接口(后台接口)  '未选中：0 选中：1'
     */
    @ApiOperation("轮播图启用/禁用接口(后台接口)  '未选中：0 选中：1'")
    @PutMapping("{id}/{state}")
    public R carShelve(@PathVariable Long id,@PathVariable Integer state){
        carLunboService.carLunboShelve(id,state);
        return R.ok();
    }
}
